// withdrawals.js
var app = getApp()
import { formatMoney, outputmoney } from "../../utils/util.js";
import { showTi, showModal } from "../../utils/modal.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    bank: {},
    available: null,
    availableObj:null,
    money: null,
    amount:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.userInfo()
    
  },
  bindKeyInputAmount:function(e){
    this.setData({
      amount: parseInt(e.detail.value) 
    });
  },
  //提现
  withdrawals: function () {
    let self = this
    let params = {};
    if (self.data.amount){
      params.amount = parseFloat(self.data.amount) * 1000;
      params.open_bank_code = self.data.bank.open_bank;
      params.open_bank = self.data.bank.channel_title;
      params.account = self.data.bank.account;
      params.pay_type = 'bank';
      app.query("web/users/appoint_cash", params, 'post').then(res => {
        if (res.code === 200) {
          this.setData({
            amount: ''
          })
          showTi("提现成功!")
          console.log(res)
        }else{
          this.setData({
            amount: ''
          })
          showTi(res.msg)
        }
      })
    }else{
      showTi("请输入提现金额!")
    }
  },
  //删除
  delectNum:function(){
    this.setData({
      amount:''
    })
  },
  userInfo:function(){
    app.query("web/users/owner_info").then(res=>{
      if (res.code === 200){
        let availablemony=formatMoney(res.user.amount, true)
        this.setData({
          user: res.user,
          available: formatMoney(res.user.amount, true),
          availableObj: outputmoney(availablemony)
        })
        this.formatBank(this.data.user)
        console.log(this.data.user)
      }
    })
  },
  //是否绑定银行卡
  formatBank(user) {
    console.log(user)
    let self = this;
    if (user.deposit_accounts.length != 0) {
      for (var i in user.deposit_accounts) {
        if (user.deposit_accounts[i].channel_type == 'bank') {
          self.setData({
            bank: self.data.user.deposit_accounts[i]
          });
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})